"""
XPath解析
"""
from lxml import etree
import requests

resp = requests.get(
    url='https://movie.douban.com/top250',
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
    }
)

# 将页面生成一个Element对象（根节点）
root = etree.HTML(resp.text)
spans = root.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[1]/a/span[1]')
hrefs = root.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[1]/a/@href')
for span, href in zip(spans, hrefs):
    print(span.text, href)
